<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    
    <title>Jenkins API 使用 | jouyouyun&#39;s blog</title>
    <meta name="viewport" content="width=device-width,minimum-scale=1">
    <meta name="description" content="Jenkins 是一款流行的开源持续集成工具，可以用来做一些软件开发的自动化工作，如打包，测试，自动部署等。
Jenkins 中有 view 和 job 的概念， view 相当于组， job 则是具体的任务。
view 下面可以创建 job ，但 job 可以不在任何 view 下。
这里主要介绍 Jenkins 提供的 HTTP API ，至于如何使用 Jenkins 请参看 Jenkins User Documentation。">
    <meta name="generator" content="Hugo 0.105.0">
    
    
    
    
      <meta name="robots" content="noindex, nofollow">
    

    
<link rel="stylesheet" href="/ananke/css/main.min.css" >



    
    
    
      

    

    
    
    <meta property="og:title" content="Jenkins API 使用" />
<meta property="og:description" content="Jenkins 是一款流行的开源持续集成工具，可以用来做一些软件开发的自动化工作，如打包，测试，自动部署等。
Jenkins 中有 view 和 job 的概念， view 相当于组， job 则是具体的任务。
view 下面可以创建 job ，但 job 可以不在任何 view 下。
这里主要介绍 Jenkins 提供的 HTTP API ，至于如何使用 Jenkins 请参看 Jenkins User Documentation。" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://jouyouyun.github.io/post/jenkins-api-usage/" /><meta property="article:section" content="post" />
<meta property="article:published_time" content="2019-04-21T20:02:00+08:00" />
<meta property="article:modified_time" content="2019-05-16T16:26:15+08:00" />

<meta itemprop="name" content="Jenkins API 使用">
<meta itemprop="description" content="Jenkins 是一款流行的开源持续集成工具，可以用来做一些软件开发的自动化工作，如打包，测试，自动部署等。
Jenkins 中有 view 和 job 的概念， view 相当于组， job 则是具体的任务。
view 下面可以创建 job ，但 job 可以不在任何 view 下。
这里主要介绍 Jenkins 提供的 HTTP API ，至于如何使用 Jenkins 请参看 Jenkins User Documentation。"><meta itemprop="datePublished" content="2019-04-21T20:02:00+08:00" />
<meta itemprop="dateModified" content="2019-05-16T16:26:15+08:00" />
<meta itemprop="wordCount" content="676">
<meta itemprop="keywords" content="jenkins,rest,api," /><meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="Jenkins API 使用"/>
<meta name="twitter:description" content="Jenkins 是一款流行的开源持续集成工具，可以用来做一些软件开发的自动化工作，如打包，测试，自动部署等。
Jenkins 中有 view 和 job 的概念， view 相当于组， job 则是具体的任务。
view 下面可以创建 job ，但 job 可以不在任何 view 下。
这里主要介绍 Jenkins 提供的 HTTP API ，至于如何使用 Jenkins 请参看 Jenkins User Documentation。"/>

	
  </head>

  <body class="ma0 avenir bg-near-white">

    
   
  

  <header>
    <div class="bg-black">
      <nav class="pv3 ph3 ph4-ns" role="navigation">
  <div class="flex-l justify-between items-center center">
    <a href="/" class="f3 fw2 hover-white no-underline white-90 dib">
      
        jouyouyun&#39;s blog
      
    </a>
    <div class="flex-l items-center">
      

      
        <ul class="pl0 mr3">
          
          <li class="list f5 f4-ns fw4 dib pr3">
            <a class="hover-white no-underline white-90" href="/post/" title="Archives 页">
              Archives
            </a>
          </li>
          
          <li class="list f5 f4-ns fw4 dib pr3">
            <a class="hover-white no-underline white-90" href="/tags/" title="Tags 页">
              Tags
            </a>
          </li>
          
          <li class="list f5 f4-ns fw4 dib pr3">
            <a class="hover-white no-underline white-90" href="/categories/" title="Categories 页">
              Categories
            </a>
          </li>
          
        </ul>
      
      
<div class="ananke-socials">
  
    <a href="https://github.com/jouyouyun" target="_blank" class="github ananke-social-link link-transition stackoverflow link dib z-999 pt3 pt0-l mr1" title="GitHub link" rel="noopener" aria-label="follow on GitHub——Opens in a new window">
      
        <span class="icon"><svg style="enable-background:new 0 0 512 512;" version="1.1" viewBox="0 0 512 512"  xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" >
  <path d="M256,32C132.3,32,32,134.8,32,261.7c0,101.5,64.2,187.5,153.2,217.9c11.2,2.1,15.3-5,15.3-11.1   c0-5.5-0.2-19.9-0.3-39.1c-62.3,13.9-75.5-30.8-75.5-30.8c-10.2-26.5-24.9-33.6-24.9-33.6c-20.3-14.3,1.5-14,1.5-14   c22.5,1.6,34.3,23.7,34.3,23.7c20,35.1,52.4,25,65.2,19.1c2-14.8,7.8-25,14.2-30.7c-49.7-5.8-102-25.5-102-113.5   c0-25.1,8.7-45.6,23-61.6c-2.3-5.8-10-29.2,2.2-60.8c0,0,18.8-6.2,61.6,23.5c17.9-5.1,37-7.6,56.1-7.7c19,0.1,38.2,2.6,56.1,7.7   c42.8-29.7,61.5-23.5,61.5-23.5c12.2,31.6,4.5,55,2.2,60.8c14.3,16.1,23,36.6,23,61.6c0,88.2-52.4,107.6-102.3,113.3   c8,7.1,15.2,21.1,15.2,42.5c0,30.7-0.3,55.5-0.3,63c0,6.1,4,13.3,15.4,11C415.9,449.1,480,363.1,480,261.7   C480,134.8,379.7,32,256,32z"/>
</svg>
</span>
      
<span class="new-window"><svg  height="8px"  style="enable-background:new 0 0 1000 1000;" version="1.1" viewBox="0 0 1000 1000"  xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" >
<path d="M598 128h298v298h-86v-152l-418 418-60-60 418-418h-152v-86zM810 810v-298h86v298c0 46-40 86-86 86h-596c-48 0-86-40-86-86v-596c0-46 38-86 86-86h298v86h-298v596h596z" style="fill-rule:evenodd;clip-rule:evenodd;"/>
</svg>
</span></a>
  
</div>

    </div>
  </div>
</nav>

    </div>
  </header>



    <main class="pb7" role="main">
      
  
  <article class="flex-l flex-wrap justify-between mw8 center ph3">
    <header class="mt4 w-100">
      <aside class="instapaper_ignoref b helvetica tracked">
          
        POSTS
      </aside>
      










  <div id="sharing" class="mt3 ananke-socials">
    
  </div>


      <h1 class="f1 athelas mt3 mb1">Jenkins API 使用</h1>
      
      <p class="tracked">
         <strong>jouyouyun</strong>
      </p>
      
      
      
      <time class="f6 mv4 dib tracked" datetime="2019-04-21T20:02:00+08:00">四月 21, 2019</time>
      

      
      
    </header>
    <div class="nested-copy-line-height lh-copy serif f4 nested-links mid-gray pr4-l w-two-thirds-l"><p><code>Jenkins</code> 是一款流行的开源持续集成工具，可以用来做一些软件开发的自动化工作，如打包，测试，自动部署等。</p>
<p><code>Jenkins</code> 中有 <code>view</code> 和 <code>job</code> 的概念， <code>view</code> 相当于组， <code>job</code> 则是具体的任务。
<code>view</code> 下面可以创建 <code>job</code> ，但 <code>job</code> 可以不在任何 <code>view</code> 下。</p>
<p>这里主要介绍 <code>Jenkins</code> 提供的 <code>HTTP API</code> ，至于如何使用 <code>Jenkins</code> 请参看 <a href="https://jenkins.io/doc/">Jenkins User Documentation</a>。</p>
<h2 id="api">API</h2>
<h3 id="鉴权">鉴权</h3>
<p><code>Jenkins</code> 使用 <code>Baisc Auth</code> 的权限验证方式，需要传入 <code>username</code> 和 <code>api token</code> 。
其中 <code>api token</code> 需要在用户的设置界面去创建。</p>
<p>但在 <code>Job</code> 的远程触发中，可以设置用于远程触发的 <code>token</code> (在 <code>Job</code> 的配置页面设置)，这样在触发 <code>Job</code> 时就不需要传入 <code>Basic Auth</code> 了。
远程触发的 <code>token</code> 使用 <code>urlencode</code> 的方式放在请求的 <code>body</code> 中，其原始数据为： <code>token=&lt;Token Value&gt;</code></p>
<p>下面给出两种方式触发 <code>Job</code> 的例子：</p>
<ul>
<li>
<p><strong>Basic Auth</strong></p>
<p><code>curl -X POST &lt;jenkins url&gt;/view/&lt;view name&gt;/job/&lt;job name&gt;/build --user &lt;username&gt;:&lt;api token&gt;</code></p>
</li>
<li>
<p><strong>Token</strong></p>
<p><code>curl -X POST &lt;jenkins url&gt;/view/&lt;view name&gt;/job/&lt;job name&gt;/build --data-urlencode token=&lt;Token Value&gt;</code></p>
</li>
</ul>
<h3 id="view">View</h3>
<ul>
<li>
<p>创建</p>
<p><strong>API:</strong> <code>&lt;jenkins url&gt;/createView</code></p>
<p><strong>curl:</strong> <code>curl -X POST &lt;jenkins url&gt;/createView?name=&lt;view name&gt; --data-binary &quot;@viewCofnig.xml&quot; -H &quot;Content-Type: text/xml&quot; --auth &lt;u&gt;:&lt;p&gt;</code></p>
</li>
<li>
<p>删除</p>
<p><strong>API:</strong> <code>&lt;jenkins url&gt;/view/&lt;view name&gt;/doDelete</code></p>
<p><strong>curl:</strong> <code>curl -X POST &lt;jenkins url&gt;/view/&lt;view name&gt;/doDelete --auth &lt;u&gt;:&lt;p&gt;</code></p>
</li>
</ul>
<!-- raw HTML omitted -->
<ul>
<li>
<p>查询状态</p>
<p><strong>API:</strong> <code>&lt;jenkins url&gt;/view/&lt;view name&gt;/api/json</code></p>
<p><strong>curl:</strong> <code>curl -X GET &lt;jenkins url&gt;/view/&lt;view name&gt;/api/json --auth &lt;u&gt;:&lt;p&gt;</code></p>
</li>
<li>
<p>查询配置</p>
<p><strong>API:</strong> <code>&lt;jenkins url&gt;/view/&lt;view name&gt;/config.xml</code></p>
<p><strong>curl:</strong> <code>curl -X GET &lt;jenkins url&gt;/view/&lt;view name&gt;/config.xml --auth &lt;u&gt;:&lt;p&gt;</code></p>
</li>
</ul>
<!-- raw HTML omitted -->
<ul>
<li>
<p>更新配置</p>
<p><strong>API:</strong> <code>&lt;jenkins url&gt;/view/&lt;view name&gt;/config.xml</code></p>
<p><strong>curl:</strong> <code>curl -X POST &lt;jenkins url&gt;/view/&lt;view name&gt;/config.xml --data-binary &quot;@newCofnig.xml&quot; -H &quot;Content-Type: text/xml&quot; --auth &lt;u&gt;:&lt;p&gt;</code></p>
</li>
</ul>
<h3 id="job">Job</h3>
<ul>
<li>
<p>创建</p>
<p><strong>API:</strong> <code>&lt;jenkins url&gt;/createItem</code></p>
<p><strong>curl:</strong> <code>curl -X POST &lt;jenkins url&gt;/createItem?name=&lt;job name&gt; --data-binary &quot;@jobConfig.xml&quot; -H &quot;Content-Type: text/xml&quot; --auth &lt;u&gt;:&lt;p&gt;</code></p>
</li>
<li>
<p>删除</p>
<p><strong>API:</strong> <code>&lt;jenkins url&gt;/job/&lt;job name&gt;/doDelete</code></p>
<p><strong>curl:</strong> <code>curl -X POST &lt;jenkins url&gt;/job/&lt;job name&gt;/doDelete --auth &lt;u&gt;:&lt;p&gt;</code></p>
</li>
<li>
<p>查询状态</p>
<p><strong>API:</strong> <code>&lt;jenkins url&gt;/view/&lt;view name&gt;/job/&lt;job name&gt;/api/json</code></p>
<p><strong>curl:</strong> <code>curl -X GET &lt;jenkins url&gt;/view/&lt;view name&gt;/job/&lt;job name&gt;/api/json --auth &lt;u&gt;:&lt;p&gt;</code></p>
</li>
<li>
<p>启用</p>
<p><strong>API:</strong> <code>&lt;jenkins url&gt;/view/&lt;view name&gt;/job/&lt;job name&gt;/enable</code></p>
</li>
<li>
<p>禁用</p>
<p><strong>API:</strong> <code>&lt;jenkins url&gt;/view/&lt;view name&gt;/job/&lt;job name&gt;/disable</code></p>
</li>
<li>
<p>查询配置</p>
<p><strong>API:</strong> <code>&lt;jenkins url&gt;/view/&lt;view name&gt;/job/&lt;job name&gt;/config.xml</code></p>
<p><strong>curl:</strong> <code>curl -X GET &lt;jenkins url&gt;/view/&lt;view name&gt;/job/&lt;job name&gt;/config.xml --auth &lt;u&gt;:&lt;p&gt;</code></p>
</li>
</ul>
<!-- raw HTML omitted -->
<ul>
<li>
<p>更新配置</p>
<p><strong>API:</strong> <code>&lt;jenkins url&gt;/view/&lt;view name&gt;/job/&lt;job name&gt;/config.xml</code></p>
<p><strong>curl:</strong> <code>curl -X POST &lt;jenkins url&gt;/view/&lt;view name&gt;/job/&lt;job name&gt;/config.xml --data-binary &quot;@newCofnig.xml&quot; -H &quot;Content-Type: text/xml&quot; --auth &lt;u&gt;:&lt;p&gt;</code></p>
</li>
</ul>
<h3 id="job-build">Job Build</h3>
<p>为了描述方便，这里先定义 <code>baseURL=&lt;jenkins url&gt;/view/&lt;view name&gt;/job/&lt;job name&gt;</code> ，下面的 <code>API</code> 都需要加上 <code>baseURL</code> 才是完整的 =URL=。</p>
<ul>
<li>
<p>触发构建</p>
<p><strong>API:</strong> <code>/build</code></p>
<p>支持 <code>token</code> 触发，支持填入构建参数，构建参数是在 <code>Job</code> 配置页面创建的。</p>
<p>如使用 <code>token</code> 并有字符参数 <code>branch</code> 和 <code>target</code> 的例子：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span>curl -X POST &lt;api&gt; --data-urlencode token<span style="color:#f92672">=</span>&lt;token value&gt; <span style="color:#ae81ff">\
</span></span></span><span style="display:flex;"><span><span style="color:#ae81ff"></span>--data-urlencode json<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;{&#34;parameters&#34;: [{&#34;name&#34;: &#34;branch&#34;, &#34;value&#34;: &#34;master&#34;},{&#34;name&#34;:&#34;target&#34;,&#34;value&#34;:&#34;mirror&#34;}]}&#39;</span>
</span></span></code></pre></div><p>触发成功后，会在 <code>header</code> 的 <code>Location</code> 字段指明 <code>queue url</code> ，再通过查询 <code>queue</code> 就可获取到 <code>build id</code> 了。</p>
</li>
<li>
<p>停止构建</p>
<p><strong>API:</strong> <code>/&lt;id&gt;/stop</code></p>
</li>
<li>
<p>删除构建</p>
<p><strong>API:</strong> <code>/&lt;id&gt;/doDelete</code></p>
</li>
<li>
<p>构建状态</p>
<p><strong>API:</strong> <code>/&lt;id&gt;/api/json</code></p>
<p><strong>进度说明:</strong> 返回的信息中， <code>timestamp</code> 表示开始时间， <code>estimatedDuration</code> 表示预估的任务耗时，这两个时间再加上当前时间就可估算任务进度。另外 <code>duration</code> 任务实际的耗时，信息中时间的单位都是毫秒(<code>ms</code>)。</p>
</li>
<li>
<p>最后一次构建</p>
<p><strong>API:</strong> <code>/lastBuild/api/json</code></p>
</li>
</ul>
<h2 id="附录">附录</h2>
<h3 id="使用-go-编写触发构建的程序">使用 <code>Go</code> 编写触发构建的程序</h3>
<p>这里只给出关键代码：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#75715e">// QueueInfo jenkins return&#39;s info by queue json api
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">type</span> <span style="color:#a6e22e">QueueInfo</span> <span style="color:#66d9ef">struct</span> {
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">Executable</span> <span style="color:#66d9ef">struct</span> {
</span></span><span style="display:flex;"><span>                <span style="color:#a6e22e">Number</span> <span style="color:#66d9ef">int</span>    <span style="color:#e6db74">`json:&#34;number&#34;`</span>
</span></span><span style="display:flex;"><span>                <span style="color:#a6e22e">URL</span>    <span style="color:#66d9ef">string</span> <span style="color:#e6db74">`json:&#34;url&#34;`</span>
</span></span><span style="display:flex;"><span>        } <span style="color:#e6db74">`json:&#34;Executable&#34;`</span>
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">var</span> (
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">jenkinsURL</span>   = <span style="color:#a6e22e">flag</span>.<span style="color:#a6e22e">String</span>(<span style="color:#e6db74">&#34;url&#34;</span>, <span style="color:#e6db74">&#34;https://ci.deepin.io&#34;</span>, <span style="color:#e6db74">&#34;Jenkins site url&#34;</span>)
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">jenkinsView</span>  = <span style="color:#a6e22e">flag</span>.<span style="color:#a6e22e">String</span>(<span style="color:#e6db74">&#34;view&#34;</span>, <span style="color:#e6db74">&#34;&#34;</span>, <span style="color:#e6db74">&#34;Jenkins job view&#34;</span>)
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">jenkinsJob</span>   = <span style="color:#a6e22e">flag</span>.<span style="color:#a6e22e">String</span>(<span style="color:#e6db74">&#34;job&#34;</span>, <span style="color:#e6db74">&#34;&#34;</span>, <span style="color:#e6db74">&#34;Jenkins job name&#34;</span>)
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">jenkinsToken</span> = <span style="color:#a6e22e">flag</span>.<span style="color:#a6e22e">String</span>(<span style="color:#e6db74">&#34;token&#34;</span>, <span style="color:#e6db74">&#34;&#34;</span>, <span style="color:#e6db74">&#34;Jenkins job token&#34;</span>)
</span></span><span style="display:flex;"><span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">// BuildJob trigger a job build
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">BuildJob</span>(<span style="color:#a6e22e">params</span> <span style="color:#66d9ef">map</span>[<span style="color:#66d9ef">string</span>]<span style="color:#66d9ef">string</span>) (<span style="color:#66d9ef">int</span>, <span style="color:#66d9ef">error</span>) {
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">var</span> <span style="color:#a6e22e">api</span> = <span style="color:#f92672">*</span><span style="color:#a6e22e">jenkinsURL</span>
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">if</span> len(<span style="color:#f92672">*</span><span style="color:#a6e22e">jenkinsView</span>) <span style="color:#f92672">!=</span> <span style="color:#ae81ff">0</span> {
</span></span><span style="display:flex;"><span>                <span style="color:#a6e22e">api</span> <span style="color:#f92672">+=</span> <span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Sprintf</span>(<span style="color:#e6db74">&#34;/view/%s&#34;</span>, <span style="color:#f92672">*</span><span style="color:#a6e22e">jenkinsView</span>)
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">if</span> len(<span style="color:#f92672">*</span><span style="color:#a6e22e">jenkinsJob</span>) <span style="color:#f92672">!=</span> <span style="color:#ae81ff">0</span> {
</span></span><span style="display:flex;"><span>                <span style="color:#a6e22e">api</span> <span style="color:#f92672">+=</span> <span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Sprintf</span>(<span style="color:#e6db74">&#34;/job/%s&#34;</span>, <span style="color:#f92672">*</span><span style="color:#a6e22e">jenkinsJob</span>)
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">api</span> <span style="color:#f92672">+=</span> <span style="color:#e6db74">&#34;/build&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>        <span style="color:#75715e">// params must encode by url
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>        <span style="color:#66d9ef">var</span> <span style="color:#a6e22e">values</span> = make(<span style="color:#a6e22e">url</span>.<span style="color:#a6e22e">Values</span>)
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">for</span> <span style="color:#a6e22e">k</span>, <span style="color:#a6e22e">v</span> <span style="color:#f92672">:=</span> <span style="color:#66d9ef">range</span> <span style="color:#a6e22e">params</span> {
</span></span><span style="display:flex;"><span>                <span style="color:#a6e22e">values</span>.<span style="color:#a6e22e">Set</span>(<span style="color:#a6e22e">k</span>, <span style="color:#a6e22e">v</span>)
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">req</span>, <span style="color:#a6e22e">err</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">http</span>.<span style="color:#a6e22e">NewRequest</span>(<span style="color:#a6e22e">http</span>.<span style="color:#a6e22e">MethodPost</span>, <span style="color:#a6e22e">api</span>,
</span></span><span style="display:flex;"><span>                <span style="color:#a6e22e">bytes</span>.<span style="color:#a6e22e">NewBufferString</span>(<span style="color:#a6e22e">values</span>.<span style="color:#a6e22e">Encode</span>()))
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">if</span> <span style="color:#a6e22e">err</span> <span style="color:#f92672">!=</span> <span style="color:#66d9ef">nil</span> {
</span></span><span style="display:flex;"><span>                <span style="color:#66d9ef">return</span> <span style="color:#f92672">-</span><span style="color:#ae81ff">1</span>, <span style="color:#a6e22e">err</span>
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>        <span style="color:#75715e">// must set &#39;Content-Type&#39; to &#39;application/x-www-form-urlencoded&#39;
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>        <span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">Header</span>.<span style="color:#a6e22e">Set</span>(<span style="color:#e6db74">&#34;Content-Type&#34;</span>, <span style="color:#e6db74">&#34;application/x-www-form-urlencoded; charset=utf-8&#34;</span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">resp</span>, <span style="color:#a6e22e">err</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">http</span>.<span style="color:#a6e22e">DefaultClient</span>.<span style="color:#a6e22e">Do</span>(<span style="color:#a6e22e">req</span>)
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">if</span> <span style="color:#a6e22e">err</span> <span style="color:#f92672">!=</span> <span style="color:#66d9ef">nil</span> {
</span></span><span style="display:flex;"><span>                <span style="color:#66d9ef">return</span> <span style="color:#f92672">-</span><span style="color:#ae81ff">1</span>, <span style="color:#a6e22e">err</span>
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">if</span> <span style="color:#a6e22e">resp</span>.<span style="color:#a6e22e">Body</span> <span style="color:#f92672">!=</span> <span style="color:#66d9ef">nil</span> {
</span></span><span style="display:flex;"><span>                <span style="color:#66d9ef">defer</span> <span style="color:#a6e22e">resp</span>.<span style="color:#a6e22e">Body</span>.<span style="color:#a6e22e">Close</span>()
</span></span><span style="display:flex;"><span>                <span style="color:#a6e22e">data</span>, <span style="color:#a6e22e">err</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">ioutil</span>.<span style="color:#a6e22e">ReadAll</span>(<span style="color:#a6e22e">resp</span>.<span style="color:#a6e22e">Body</span>)
</span></span><span style="display:flex;"><span>                <span style="color:#66d9ef">if</span> <span style="color:#a6e22e">err</span> <span style="color:#f92672">!=</span> <span style="color:#66d9ef">nil</span> {
</span></span><span style="display:flex;"><span>                        <span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#e6db74">&#34;Failed to read response content:&#34;</span>, <span style="color:#a6e22e">err</span>)
</span></span><span style="display:flex;"><span>                } <span style="color:#66d9ef">else</span> {
</span></span><span style="display:flex;"><span>                        <span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#e6db74">&#34;Response content:&#34;</span>, string(<span style="color:#a6e22e">data</span>))
</span></span><span style="display:flex;"><span>                }
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#e6db74">&#34;Status:&#34;</span>, <span style="color:#a6e22e">resp</span>.<span style="color:#a6e22e">Status</span>)
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">if</span> <span style="color:#a6e22e">resp</span>.<span style="color:#a6e22e">StatusCode</span> &lt; <span style="color:#ae81ff">200</span> <span style="color:#f92672">||</span> <span style="color:#a6e22e">resp</span>.<span style="color:#a6e22e">StatusCode</span> <span style="color:#f92672">&gt;=</span> <span style="color:#ae81ff">300</span> {
</span></span><span style="display:flex;"><span>                <span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#e6db74">&#34;Failed to build job&#34;</span>)
</span></span><span style="display:flex;"><span>                <span style="color:#66d9ef">return</span> <span style="color:#f92672">-</span><span style="color:#ae81ff">1</span>, <span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Errorf</span>(<span style="color:#e6db74">&#34;build job failure&#34;</span>)
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#e6db74">&#34;Response headers:&#34;</span>, <span style="color:#a6e22e">resp</span>.<span style="color:#a6e22e">Header</span>)
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">queueAPI</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">resp</span>.<span style="color:#a6e22e">Header</span>.<span style="color:#a6e22e">Get</span>(<span style="color:#e6db74">&#34;Location&#34;</span>)
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">queue</span>, <span style="color:#a6e22e">err</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">GetQueue</span>(<span style="color:#a6e22e">queueAPI</span>)
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">if</span> <span style="color:#a6e22e">err</span> <span style="color:#f92672">!=</span> <span style="color:#66d9ef">nil</span> {
</span></span><span style="display:flex;"><span>                <span style="color:#66d9ef">return</span> <span style="color:#f92672">-</span><span style="color:#ae81ff">1</span>, <span style="color:#a6e22e">err</span>
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">return</span> <span style="color:#a6e22e">queue</span>.<span style="color:#a6e22e">Executable</span>.<span style="color:#a6e22e">Number</span>, <span style="color:#66d9ef">nil</span>
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">GetQueue</span>(<span style="color:#a6e22e">api</span> <span style="color:#66d9ef">string</span>) (<span style="color:#f92672">*</span><span style="color:#a6e22e">QueueInfo</span>, <span style="color:#66d9ef">error</span>) {
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">resp</span>, <span style="color:#a6e22e">err</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">http</span>.<span style="color:#a6e22e">Get</span>(<span style="color:#a6e22e">api</span> <span style="color:#f92672">+</span> <span style="color:#e6db74">&#34;/api/json&#34;</span>)
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">if</span> <span style="color:#a6e22e">err</span> <span style="color:#f92672">!=</span> <span style="color:#66d9ef">nil</span> {
</span></span><span style="display:flex;"><span>                <span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#e6db74">&#34;Failed to get queue:&#34;</span>, <span style="color:#a6e22e">err</span>)
</span></span><span style="display:flex;"><span>                <span style="color:#66d9ef">return</span> <span style="color:#66d9ef">nil</span>, <span style="color:#a6e22e">err</span>
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">defer</span> <span style="color:#a6e22e">resp</span>.<span style="color:#a6e22e">Body</span>.<span style="color:#a6e22e">Close</span>()
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">data</span>, <span style="color:#a6e22e">err</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">ioutil</span>.<span style="color:#a6e22e">ReadAll</span>(<span style="color:#a6e22e">resp</span>.<span style="color:#a6e22e">Body</span>)
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">if</span> <span style="color:#a6e22e">err</span> <span style="color:#f92672">!=</span> <span style="color:#66d9ef">nil</span> {
</span></span><span style="display:flex;"><span>                <span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#e6db74">&#34;Failed to read response content:&#34;</span>, <span style="color:#a6e22e">err</span>)
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">if</span> <span style="color:#a6e22e">resp</span>.<span style="color:#a6e22e">StatusCode</span> &lt; <span style="color:#ae81ff">200</span> <span style="color:#f92672">||</span> <span style="color:#a6e22e">resp</span>.<span style="color:#a6e22e">StatusCode</span> <span style="color:#f92672">&gt;=</span> <span style="color:#ae81ff">300</span> {
</span></span><span style="display:flex;"><span>                <span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#e6db74">&#34;Failed to get queue info&#34;</span>)
</span></span><span style="display:flex;"><span>                <span style="color:#66d9ef">return</span> <span style="color:#66d9ef">nil</span>, <span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Errorf</span>(<span style="color:#e6db74">&#34;get queue info failure&#34;</span>)
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">var</span> <span style="color:#a6e22e">info</span> <span style="color:#a6e22e">QueueInfo</span>
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">err</span> = <span style="color:#a6e22e">json</span>.<span style="color:#a6e22e">Unmarshal</span>(<span style="color:#a6e22e">data</span>, <span style="color:#f92672">&amp;</span><span style="color:#a6e22e">info</span>)
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">if</span> <span style="color:#a6e22e">err</span> <span style="color:#f92672">!=</span> <span style="color:#66d9ef">nil</span> {
</span></span><span style="display:flex;"><span>                <span style="color:#66d9ef">return</span> <span style="color:#66d9ef">nil</span>, <span style="color:#a6e22e">err</span>
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">return</span> <span style="color:#f92672">&amp;</span><span style="color:#a6e22e">info</span>, <span style="color:#66d9ef">nil</span>
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><h3 id="参考资料">参考资料</h3>
<p>没有在 <a href="https://wiki.jenkins.io/display/JENKINS/Remote+access+API">Jenkins REST API 文档</a> 中找到过多的 <code>API</code> 信息，但在 <a href="https://opendev.org/jjb/python-jenkins">python-jenkins</a> 的 <a href="https://opendev.org/jjb/python-jenkins/src/branch/master/jenkins/%5F%5Finit%5F%5F.py">代码</a> 中找到，所以想要了解更多请看代码。</p><ul class="pa0">
  
   <li class="list di">
     <a href="/tags/jenkins" class="link f5 grow no-underline br-pill ba ph3 pv2 mb2 dib black sans-serif">jenkins</a>
   </li>
  
   <li class="list di">
     <a href="/tags/rest" class="link f5 grow no-underline br-pill ba ph3 pv2 mb2 dib black sans-serif">rest</a>
   </li>
  
   <li class="list di">
     <a href="/tags/api" class="link f5 grow no-underline br-pill ba ph3 pv2 mb2 dib black sans-serif">api</a>
   </li>
  
</ul>
<div class="mt6 instapaper_ignoref">
      
      
      </div>
    </div>

    <aside class="w-30-l mt6-l">




</aside>

  </article>

    </main>
    <footer class="bg-black bottom-0 w-100 pa3" role="contentinfo">
  <div class="flex justify-between">
  <a class="f4 fw4 hover-white no-underline white-70 dn dib-ns pv2 ph3" href="https://jouyouyun.github.io" >
    &copy;  jouyouyun 2023 
  </a>
    <div>
<div class="ananke-socials">
  
    <a href="https://github.com/jouyouyun" target="_blank" class="github ananke-social-link link-transition stackoverflow link dib z-999 pt3 pt0-l mr1" title="GitHub link" rel="noopener" aria-label="follow on GitHub——Opens in a new window">
      
        <span class="icon"><svg style="enable-background:new 0 0 512 512;" version="1.1" viewBox="0 0 512 512"  xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" >
  <path d="M256,32C132.3,32,32,134.8,32,261.7c0,101.5,64.2,187.5,153.2,217.9c11.2,2.1,15.3-5,15.3-11.1   c0-5.5-0.2-19.9-0.3-39.1c-62.3,13.9-75.5-30.8-75.5-30.8c-10.2-26.5-24.9-33.6-24.9-33.6c-20.3-14.3,1.5-14,1.5-14   c22.5,1.6,34.3,23.7,34.3,23.7c20,35.1,52.4,25,65.2,19.1c2-14.8,7.8-25,14.2-30.7c-49.7-5.8-102-25.5-102-113.5   c0-25.1,8.7-45.6,23-61.6c-2.3-5.8-10-29.2,2.2-60.8c0,0,18.8-6.2,61.6,23.5c17.9-5.1,37-7.6,56.1-7.7c19,0.1,38.2,2.6,56.1,7.7   c42.8-29.7,61.5-23.5,61.5-23.5c12.2,31.6,4.5,55,2.2,60.8c14.3,16.1,23,36.6,23,61.6c0,88.2-52.4,107.6-102.3,113.3   c8,7.1,15.2,21.1,15.2,42.5c0,30.7-0.3,55.5-0.3,63c0,6.1,4,13.3,15.4,11C415.9,449.1,480,363.1,480,261.7   C480,134.8,379.7,32,256,32z"/>
</svg>
</span>
      
<span class="new-window"><svg  height="8px"  style="enable-background:new 0 0 1000 1000;" version="1.1" viewBox="0 0 1000 1000"  xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" >
<path d="M598 128h298v298h-86v-152l-418 418-60-60 418-418h-152v-86zM810 810v-298h86v298c0 46-40 86-86 86h-596c-48 0-86-40-86-86v-596c0-46 38-86 86-86h298v86h-298v596h596z" style="fill-rule:evenodd;clip-rule:evenodd;"/>
</svg>
</span></a>
  
</div>
</div>
  </div>
</footer>

  </body>
</html>
